<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item>{{ $t('Home') }}</el-breadcrumb-item>
    <el-breadcrumb-item>{{ $t('Community') }}</el-breadcrumb-item>
    <el-breadcrumb-item>{{ $t('DAO') }}</el-breadcrumb-item>
  </el-breadcrumb>
  <div class="dao-home">
    <main class="content">
      <section class="sidebar">
        <el-card shadow="never" class="header">
          <div class="logo-container">
            <img :src="config.getUrl(community.logo_url)" alt="Logo" class="logo" />
          </div>
          <div class="text-container">
            <div class="name">
              {{ community.community_name }}
            </div>
            <div class="description">
              {{ community.description }}
            </div>
            <div class="article-topic">
              <el-tag>社区动态</el-tag>
              <el-tag>治理提案</el-tag>
              <el-tag>技术更新</el-tag>
              <el-tag>生态建设</el-tag>
            </div>
          </div>
        </el-card>

        <div class="action-buttons">
          <el-button type="primary" @click="addNewProposal" class="action-btn">
            <el-icon><Document /></el-icon>
            新增提案
          </el-button>
          <el-button type="primary" @click="addNewArticle" class="action-btn">
            <el-icon><Plus /></el-icon>
            新增文章
          </el-button>
        </div>

        <!-- 管理员名单 -->
        <div class="admin-list">
          <div v-for="admin in admins" :key="admin.id" class="admin-item">
            <div class="admin-avatar">
              <img :src="admin.avatar" alt="Admin Avatar" class="avatar" />
            </div>
            <div class="admin-name">{{ admin.name }}</div>
          </div>
        </div>
      </section>
      
      <section class="articles">
        <div v-for="article in articles" :key="article.post_id" class="article-item">
          <div class="article-cover">
            <img :src="article.cover_url"/>
          </div>
          <div class="article-description">
            <div class="article-title">{{ article.title }}</div>
            <div class="article-summary">{{ article.summary }}</div>
            <div class="article-footer">
              <div class="article-time">{{ article.created_at }}</div>
              <router-link :to="`/post/${article.post_id}`" class="read-more">阅读更多</router-link>
            </div>
          </div>
        </div>
      </section>

      <section class="votes">
        <el-scrollbar height="100%">
          <div v-for="vote in votes" :key="vote.id" class="vote-item">
            <div class="vote-name">{{ vote.name }}</div>
            <div class="vote-content">{{ vote.content }}</div>
            <div class="vote-time">{{ vote.time }}</div>
            <div class="vote-footer">
              <div class="vote-counts">
                <span>赞成: {{ vote.forCount }}</span>
                <span>反对: {{ vote.againstCount }}</span>
              </div>
              <div class="vote-buttons">
                <el-popconfirm title="Are you agree?" @confirm="voteFor(vote.id)">
                  <template #reference>
                    <el-button type="success" size="small">
                      <el-icon><Check /></el-icon>
                    </el-button>
                  </template>
                </el-popconfirm>
                <el-popconfirm title="Are you against?" @confirm="voteAgainst(vote.id)">
                  <template #reference>
                    <el-button type="danger" size="small">
                      <el-icon><Close /></el-icon>
                    </el-button>
                  </template>
                </el-popconfirm>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </section>

    </main>
  </div>
</template>
  
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElButton } from 'element-plus';
import { Plus, Document } from '@element-plus/icons-vue';
import { config } from '@/config/config';
import ApiClient from '@/api/ApiClient';
import { ApiRequest, ApiRequestBody } from '@/api/ApiRequest';
import { ApiResponse } from '@/api/ApiResponse';

const router = useRouter();
// 从localStorage读取社区信息
const community = ref(JSON.parse(localStorage.getItem('community') || '{}'));

const votes = ref([
  { id: 1, name: '提案1', content: '这是提案1的内容', time: '2022-01-01', forCount: 0, againstCount: 0 },
  { id: 2, name: '提案2', content: '这是提案2的内容', time: '2022-01-01', forCount: 0, againstCount: 0 },
]);

// 定义文章接口
interface Post {
  post_id: string;
  title: string;
  summary: string;
  cover_url: string;
  created_at: string;
  content: string;
  community_id: string;
  account_address: string;
  updated_at: string;
}

// 定义返回结果接口
interface PostsResponse {
  data: Post[];
  page: number;
  page_size: number;
  total: number;
}

// 在setup中定义文章列表
const articles = ref<Post[]>([]);

// 查询文章数
const fetchArticles = async (page: number = 1, pageSize: number = 10) => {
  const methodType = 'query';
  const methodName = 'search_posts';
  const params = {
    community_id: community.value.community_id, // 假设community对象中有community_id
    page: page.toString(),
    page_size: pageSize.toString()
  };

  const request = new ApiRequest(
    methodType,
    config.dao_chain_name,
    new ApiRequestBody(
      config.dao_contract_name,
      methodName,
      params
    )
  );

  ApiClient.sendSync(request, (response: ApiResponse) => {
    try {
      const contractResult = response.data.contract_result;
      const result = JSON.parse(contractResult.result) as PostsResponse;
      
      // 处理文章数据
      articles.value = result.data.map(post => ({
        ...post,
        // 转换时间戳为可读格式
        created_at: new Date(parseInt(post.created_at) * 1000).toLocaleString(),
        // 处理IPFS链接
        cover_url: post.cover_url.replace('ipfs://', config.ipfs_gateway)
      }));
    } catch (e) {
      console.error('解析文章数据失败:', e);
    }
  });
};

// 在组件挂载时获取文章列表
onMounted(() => {
  fetchArticles();
});

const admins = ref([
  { id: 1, name: '管理员1', avatar: 'http://38.6.216.42:8080/ipfs/QmSA7aiiKyyewfFDWRew4G5GcdPEAWiJAtRxUtGnWvYivG' },
  { id: 2, name: '管理员2', avatar: 'http://38.6.216.42:8080/ipfs/QmSA7aiiKyyewfFDWRew4G5GcdPEAWiJAtRxUtGnWvYivG' },
]);

const voteFor = (id: number) => {
  const vote = votes.value.find(v => v.id === id);
  if (vote) {
    vote.forCount++;
  }
};

const voteAgainst = (id: number) => {
  const vote = votes.value.find(v => v.id === id);
  if (vote) {
    vote.againstCount++;
  }
};

const addNewProposal = () => {
  // 处理新增提案的逻辑
};

const addNewArticle = () => {
  router.push('/post_add');
};
</script>

<style scoped>
.dao-home {
  max-width: 100%;
  margin: 20px auto;
  padding: 20px;
}

.header {
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border-radius: 12px;
  padding: 20px;
  color: white;
}

.logo-container {
  width: 70px;
  height: 70px;
  margin-right: 16px;
  display: inline-block;
}

.logo {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.text-container {
  display: flex;
  flex-direction: column;
  display: inline-block;
}

.name {
  margin: 0;
  font-size: 20px;
  margin-bottom: 6px;
  overflow: hidden; 
  text-overflow: ellipsis;
  font-weight: bold;
  color: white;
}

.description {
  margin: 0;
  font-size: 14px;
  line-height: 20px;
  width: 230px;
  color: rgba(255, 255, 255, 0.8);
  overflow: hidden; 
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.right {
  display: flex;
  align-items: center;
}

.content {
  display: flex;
  justify-content: space-between;
  width: 100%;
  gap: 30px;
}

.sidebar {
  width: 320px;
}

.votes {
  width: 320px;
  margin: 0 20px;
}

.articles {
  flex: 1;
  min-width: 0;
}

.admin-list {
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
}

.admin-item {
  background: white;
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  width: calc(50% - 30px);
  display: flex;
  align-items: center;
}

.admin-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

.admin-avatar {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  flex-shrink: 0;
}

.admin-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.admin-name {
  font-size: 14px;
  color: #333;
}

.article-item {
  border-bottom: 1px solid #ddd;
  padding: 10px;
  margin-bottom: 10px;
}

.article-item {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.08);
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 24px;
}

.article-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
}

.article-item:hover .article-cover img {
  transform: scale(1.05);
}

.article-cover {
  width: 120px;
  height: 120px;
  flex-shrink: 0;
  overflow: hidden;
  border-radius: 8px;
}

.article-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.article-description {
  flex: 1;
  min-width: 0;
  height: 120px;
  padding: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.article-title {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 8px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-summary {
  font-size: 14px;
  line-height: 1.5;
  color: #5c6b7f;
  margin-bottom: 12px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex-grow: 1;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: auto;
}

.article-time {
  color: #8492a6;
}

.read-more {
  padding: 4px 12px;
  border-radius: 16px;
  background: #e6f7ff;
  color: #1890ff;
  font-weight: 500;
  font-size: 12px;
  transition: all 0.3s;
  text-decoration: none;
  display: inline-block;
}

.vote-item {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  transition: all 0.3s;
}

.vote-name {
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 8px;
}

.vote-content {
  font-size: 14px;
  color: #5c6b7f;
  margin-bottom: 12px;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.vote-time {
  font-size: 13px;
  color: #8492a6;
  margin-bottom: 12px;
}

.vote-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.vote-counts {
  font-size: 14px;
  color: #606266;
}

.vote-counts span {
  margin-right: 16px;
}

.vote-buttons {
  display: flex;
  gap: 8px;
}

.vote-buttons .el-button {
  padding: 6px 12px;
  border-radius: 4px;
}

.vote-item,
.article-item,
.admin-item {
  transition: all 0.3s ease;
}

.vote-buttons .el-button {
  transition: all 0.2s ease;
}

.vote-buttons .el-button:hover {
  transform: scale(1.05);
}

@media (max-width: 768px) {
  .content {
    flex-direction: column;
  }
  
  .sidebar,
  .votes,
  .articles {
    width: 100%;
    margin-left: 0;
  }
  
  .article-item {
    flex-direction: column;
    padding: 16px;
  }
  
  .article-cover {
    width: 100%;
    height: 200px;
    margin-bottom: 16px;
  }
  
  .article-description {
    width: 100%;
  }
  
  .article-title {
    font-size: 18px;
  }
}

.article-topic {
  margin-top: 10px;
}

.article-topic .el-tag {
  background-color: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  margin-right: 8px;
  margin-bottom: 8px;
}

.article-topic .el-tag:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.action-buttons {
  display: flex;
  gap: 16px;
  margin: 24px 0;
  padding: 0 16px;
}

.action-btn {
  flex: 1;
  height: 44px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
  border: none;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
}

.action-btn .el-icon {
  margin-right: 6px;
  font-size: 16px;
}

/* 响应处理 */
@media (max-width: 768px) {
  .action-buttons {
    flex-direction: column;
    gap: 12px;
  }
  
  .action-btn {
    width: 100%;
  }
}
</style>